<?php
@session_start();
// 用户未登陆
if ( !isset($_SESSION['user_id']) ) {
    echo "<a href='../loginpage.php'>您还未登录,请登录后再操作!</a>";
    exit(1);
}

$isAuth = isset($_SESSION['administrator']);
$isAuth |= isset($_SESSION['problem_editor']);
if ( !$isAuth ){
    echo "你没有权限操作该页面";
    exit(1);
}

require_once ("../config/path_config.php");
require_once ("phpfm_eltag.php");
require_once (ROOTDIR."config/db_config.php");
require_once (ROOTDIR."config/sys_config.php");
require_once (ROOTDIR."common/server_utils.php");

$charset = "UTF-8";
header("Pragma: no-cache");
header("Cache-Control: no-store");
header("Content-Type: text/html; charset=".$charset);

$ip = get_client_ip();
$islinux = !(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
// 获取url信息,包括
// scheme  -- http 或者 https
// host    -- 服务器的域名
// path    -- 接口路径
// query   -- 请求参数
$url = getCompleteURL();
$url_info = parse_url($url);

// 获取服务器跟目录
$doc_root = getDocumentRoot();
//print_r($doc_root);

// 获取该文件phpfm.php所在的目录
$fm_self = $doc_root.$_SERVER["PHP_SELF"];
//print_r($fm_self);
$path_info = pathinfo($fm_self);
// print_r($path_info);

// 注册全局变量(具体作用有待确认)
$blockKeys = array('_SERVER',
    '_SESSION',
    '_GET',
    '_POST',
    '_COOKIE',
    'charset',
    'ip',
    'islinux',
    'url',
    'url_info',
    'doc_root',
    'fm_self',
    'path_info');
foreach ($_GET as $key => $val) {
    if ( !array_search( $key, $blockKeys) ) {
        $$key=$val;
    }
}
foreach ($_POST as $key => $val) {
    if ( !array_search($key, $blockKeys) ) {
        $$key=$val;
    }
}
foreach ($_COOKIE as $key => $val) {
    if ( !array_search($key, $blockKeys) ) {
        $$key=$val;
    }
}
//print_r($error_reporting);
// +--------------------------------------------------
// | phpfm运行配置
// +--------------------------------------------------
class config
{
    var $data;
    var $filename;

    public function __construct()
    {
        global $fm_self;
        $this->data = array(
            'lang'=>'en',
            'auth_pass'=>md5(''),
            'quota_mb'=>0,
            'upload_ext_filter'=>array(),
            'download_ext_filter'=>array(),
            'error_reporting'=>1,
            'fm_root'=>'',
            'cookie_cache_time'=>60*60*24*30, // 30 Days
            'version'=>'0.9.8'
        );
        /*
        $data = false;
        $this->filename = $fm_self;
        if (file_exists($this->filename)){
            $mat = file($this->filename);
            print_r($mat[1]);
            $objdata = trim(substr($mat[1],2));
            if (strlen($objdata)) {
                $data = unserialize($objdata);
            }
        }

        if (is_array($data)&&count($data)==count($this->data)){
            $this->data = $data;
        }
        else {
            $this->save();
        }*/
    }

    /*
    function save()
    {
        $objdata = "<?php".chr(13).chr(10)."//".serialize($this->data).chr(13).chr(10);
        if ( strlen($objdata) <= 0 ) {
            return;
        }

        if ( !file_exists($this->filename) ){
            return;
        }
        $mat = file($this->filename);
        if ($fh = @fopen($this->filename, "w")){
            @fputs($fh,$objdata,strlen($objdata));
            for ( $x=2; $x<count($mat); $x++ ) {
                @fputs($fh, $mat[$x], strlen($mat[$x]));
            }
            @fclose($fh);
        }
    }*/

    function load(){
        foreach ($this->data as $key => $val) {
            $GLOBALS[$key] = $val;
        }
    }
}

$cfg = new config();
$cfg->load();
// print_r($error_reporting);
switch ($error_reporting){
    case 0:
        error_reporting(0);
        //@ini_set("display_errors",0);
        break;

    case 1:
        error_reporting(E_ERROR | E_PARSE | E_COMPILE_ERROR);
        //@ini_set("display_errors",1);
        break;

    case 2:
        error_reporting(E_ALL);
        //@ini_set("display_errors",1);
        break;
}

// 获取pid和测试数据所在的目录
$pid = '';
$current_dir = '';

// 1. 获取配置的文件目录,配置文件位于config/sys_config.php
if(isset($_GET['pid'])) {
    $pid = $_GET['pid'];
    $current_dir=SysConfig::$OJ_DATA."/".$pid."/";

}
else{
    $pid=explode("/",$current_dir);
    $pid=$pid[count($pid)-2];
    $current_dir=SysConfig::$OJ_DATA."/".$pid."/";
}

// 2. 若未配置文件目录,则默认将当前目录作为文件目录
if ( $current_dir == "" ) {
    $current_dir = $path_info["dirname"]."/";
    if (!$islinux) {
        $current_dir = ucfirst($current_dir);
    }
    else {
        $current_dir = format_path($current_dir);
    }
}
// $current_dir 题目测试用例根目录
// print_r($current_dir);

// 展开当前目录
if ( !isset($expanded_dir_list) ){
    $expanded_dir_list = "";
    $mat = explode("/",$path_info["dirname"]);
    for ( $x=0; $x<count($mat); $x++ ) {
        $expanded_dir_list .= ":".$mat[$x];
    }
    print_r($expanded_dir_list);
    setcookie("expanded_dir_list", $expanded_dir_list, 0, "/");
}
// print_r($_COOKIE['expanded_dir_list']);

// 获取phpfm所在的目录
if (!isset($fm_current_root)){
    if (strlen($fm_root)) {
        $fm_current_root = $fm_root;
    }
    else {
        if (!$islinux) {
            $fm_current_root = ucfirst($path_info["dirname"]."/");
        }
        else {
            $fm_current_root = $doc_root."/";
        }
    }
    setcookie("fm_current_root", $fm_current_root, 0, "/");
}
else if (isset($set_fm_current_root)) {
    if (!$islinux) {
        $fm_current_root = ucfirst($set_fm_current_root);
    }
    setcookie("fm_current_root", $fm_current_root, 0, "/");
}
// print_r($_COOKIE['fm_current_root']);

$fm_current_root=SysConfig::$OJ_DATA;
if (!isset($resolveIDs)){
    setcookie("resolveIDs", 0, time()+$cookie_cache_time, "/");
}
else if (isset($set_resolveIDs)){
    $resolveIDs=($resolveIDs)?0:1;
    setcookie("resolveIDs", $resolveIDs, time()+$cookie_cache_time, "/");
}

if ($resolveIDs){
    exec("cat /etc/passwd",$mat_passwd);
    exec("cat /etc/group",$mat_group);
}
$fm_color['Bg'] = "EEEEEE";
$fm_color['Text'] = "000000";
$fm_color['Link'] = "0A77F7";
$fm_color['Entry'] = "FFFFFF";
$fm_color['Over'] = "C0EBFD";
$fm_color['Mark'] = "A7D2E4";
foreach($fm_color as $tag=>$color){
    $fm_color[$tag]=strtolower($color);
}

echo $path_info["basename"]." method=post ENCTYPE=multipart/form-data";

// 显示操作页面,取消登录过程
// $frame是外部传入的参数/admin/phpfm.php?frame=3&pid=1216
switch ($frame) {
    case 1:
        break; // Empty Frame
    case 2:
        frame2();
        break;
    case 3:
        frame3();
        break;
    /*
        default:
            switch($action){
                case 1:
                    logout();
                    break;

                case 2:
                    config_form();
                    break;

                case 3:
                    download();
                    break;

                case 4:
                    view();
                    break;

                case 5:
                    server_info();
                    break;

                case 6:
                    execute_cmd();
                    break;

                case 7:
                    edit_file_form();
                    break;

                case 8:
                    chmod_form();
                    break;

                case 9:
                    shell_form();
                    break;

                case 10:
                    upload_form();
                    break;

                case 11:
                    execute_file();
                    break;

                default:
                    frameset();
            }
    */

}

function frame3(){
    global $islinux,$cmd_arg,$chmod_arg,$zip_dir,$fm_current_root,$cookie_cache_time;
    global $dir_dest,$current_dir,$dir_before;
    global $selected_file_list,$selected_dir_list,$old_name,$new_name;
    global $action,$or_by,$order_dir_list_by;
    if (!isset($order_dir_list_by)){
        $order_dir_list_by = "1A";
        setcookie("order_dir_list_by", $order_dir_list_by , time()+$cookie_cache_time , "/");
    } elseif (strlen($or_by)){
        $order_dir_list_by = $or_by;
        setcookie("order_dir_list_by", $or_by , time()+$cookie_cache_time , "/");
    }
    /*
    //html_header();
    //echo "<body>\n";
    if ($action){
        switch ($action){
            case 1: // create dir
                if (strlen($cmd_arg)){
                    $cmd_arg = format_path($current_dir.$cmd_arg);
                    if (!file_exists($cmd_arg)){
                        @mkdir($cmd_arg,0755);
                        @chmod($cmd_arg,0755);
                        reloadframe("parent",2,"&ec_dir=".$cmd_arg);
                    } else alert(et('FileDirExists').".");
                }
                break;
            case 2: // create arq
                if (strlen($cmd_arg)){
                    $cmd_arg = $current_dir.$cmd_arg;
                    if (!file_exists($cmd_arg)){
                        if ($fh = @fopen($cmd_arg, "w")){
                            @fclose($fh);
                        }
                        @chmod($cmd_arg,0644);
                    } else alert(et('FileDirExists').".");
                }
                break;


            case 1002: // 创建多个测试用例文件
                if ( strlen($cmd_arg) == 0 ) {
                    break;
                }
                $testNum = intval($cmd_arg);
                for ( $i = 1; $i <= $testNum; $i++ ) {
                    // 创建输入用例文件
                    $filePath = $current_dir."test".$i.".in";
                    if ( !file_exists($filePath) ) {
                        if ($fh = @fopen($filePath, "w")){
                            @fclose($fh);
                        }
                        @chmod($filePath,0644);
                    } else {
                        alert(et('FileDirExists').".");
                    }
                    // 创建输出用例文件
                    $filePath = $current_dir."test".$i.".out";
                    if ( !file_exists($filePath) ) {
                        if ($fh = @fopen($filePath, "w")){
                            @fclose($fh);
                        }
                        @chmod($filePath,0644);
                    } else {
                        alert(et('FileDirExists').".");
                    }
                }
                break;

            case 3: // rename arq ou dir
                if ((strlen($old_name))&&(strlen($new_name))){
                    rename($current_dir.$old_name,$current_dir.$new_name);
                    if (is_dir($current_dir.$new_name)) reloadframe("parent",2);
                }
                break;
            case 4: // delete sel
                if(strstr($current_dir,$fm_current_root)){
                    if (strlen($selected_file_list)){
                        $selected_file_list = explode("<|*|>",$selected_file_list);
                        if (count($selected_file_list)) {
                            for($x=0;$x<count($selected_file_list);$x++) {
                                $selected_file_list[$x] = trim($selected_file_list[$x]);
                                if (strlen($selected_file_list[$x])) total_delete($current_dir.$selected_file_list[$x],$dir_dest.$selected_file_list[$x]);
                            }
                        }
                    }
                    if (strlen($selected_dir_list)){
                        $selected_dir_list = explode("<|*|>",$selected_dir_list);
                        if (count($selected_dir_list)) {
                            for($x=0;$x<count($selected_dir_list);$x++) {
                                $selected_dir_list[$x] = trim($selected_dir_list[$x]);
                                if (strlen($selected_dir_list[$x])) total_delete($current_dir.$selected_dir_list[$x],$dir_dest.$selected_dir_list[$x]);
                            }
                            reloadframe("parent",2);
                        }
                    }
                }
                break;
            case 5: // copy sel
                if (strlen($dir_dest)){
                    if(uppercase($dir_dest) != uppercase($current_dir)){
                        if (strlen($selected_file_list)){
                            $selected_file_list = explode("<|*|>",$selected_file_list);
                            if (count($selected_file_list)) {
                                for($x=0;$x<count($selected_file_list);$x++) {
                                    $selected_file_list[$x] = trim($selected_file_list[$x]);
                                    if (strlen($selected_file_list[$x])) total_copy($current_dir.$selected_file_list[$x],$dir_dest.$selected_file_list[$x]);
                                }
                            }
                        }
                        if (strlen($selected_dir_list)){
                            $selected_dir_list = explode("<|*|>",$selected_dir_list);
                            if (count($selected_dir_list)) {
                                for($x=0;$x<count($selected_dir_list);$x++) {
                                    $selected_dir_list[$x] = trim($selected_dir_list[$x]);
                                    if (strlen($selected_dir_list[$x])) total_copy($current_dir.$selected_dir_list[$x],$dir_dest.$selected_dir_list[$x]);
                                }
                                reloadframe("parent",2);
                            }
                        }
                        $current_dir = $dir_dest;
                    }
                }
                break;
            case 6: // move sel
                if (strlen($dir_dest)){
                    if(uppercase($dir_dest) != uppercase($current_dir)){
                        if (strlen($selected_file_list)){
                            $selected_file_list = explode("<|*|>",$selected_file_list);
                            if (count($selected_file_list)) {
                                for($x=0;$x<count($selected_file_list);$x++) {
                                    $selected_file_list[$x] = trim($selected_file_list[$x]);
                                    if (strlen($selected_file_list[$x])) total_move($current_dir.$selected_file_list[$x],$dir_dest.$selected_file_list[$x]);
                                }
                            }
                        }
                        if (strlen($selected_dir_list)){
                            $selected_dir_list = explode("<|*|>",$selected_dir_list);
                            if (count($selected_dir_list)) {
                                for($x=0;$x<count($selected_dir_list);$x++) {
                                    $selected_dir_list[$x] = trim($selected_dir_list[$x]);
                                    if (strlen($selected_dir_list[$x])) total_move($current_dir.$selected_dir_list[$x],$dir_dest.$selected_dir_list[$x]);
                                }
                                reloadframe("parent",2);
                            }
                        }
                        $current_dir = $dir_dest;
                    }
                }
                break;
            case 71: // compress sel
                if (strlen($cmd_arg)){
                    ignore_user_abort(true);
                    ini_set("display_errors",0);
                    ini_set("max_execution_time",0);
                    $zipfile=false;
                    if (strstr($cmd_arg,".tar")) $zipfile = new tar_file($cmd_arg);
                    elseif (strstr($cmd_arg,".zip")) $zipfile = new zip_file($cmd_arg);
                    elseif (strstr($cmd_arg,".bzip")) $zipfile = new bzip_file($cmd_arg);
                    elseif (strstr($cmd_arg,".gzip")) $zipfile = new gzip_file($cmd_arg);
                    if ($zipfile){
                        $zipfile->set_options(array('basedir'=>$current_dir,'overwrite'=>1,'level'=>3));
                        if (strlen($selected_file_list)){
                            $selected_file_list = explode("<|*|>",$selected_file_list);
                            if (count($selected_file_list)) {
                                for($x=0;$x<count($selected_file_list);$x++) {
                                    $selected_file_list[$x] = trim($selected_file_list[$x]);
                                    if (strlen($selected_file_list[$x])) $zipfile->add_files($selected_file_list[$x]);
                                }
                            }
                        }
                        if (strlen($selected_dir_list)){
                            $selected_dir_list = explode("<|*|>",$selected_dir_list);
                            if (count($selected_dir_list)) {
                                for($x=0;$x<count($selected_dir_list);$x++) {
                                    $selected_dir_list[$x] = trim($selected_dir_list[$x]);
                                    if (strlen($selected_dir_list[$x])) $zipfile->add_files($selected_dir_list[$x]);
                                }
                            }
                        }
                        $zipfile->create_archive();
                    }
                    unset($zipfile);
                }
                break;
            case 72: // decompress arq
                if (strlen($cmd_arg)){
                    if (file_exists($current_dir.$cmd_arg)){
                        $zipfile=false;
                        if (strstr($cmd_arg,".zip")) zip_extract();
                        elseif (strstr($cmd_arg,".bzip")||strstr($cmd_arg,".bz2")||strstr($cmd_arg,".tbz2")||strstr($cmd_arg,".bz")||strstr($cmd_arg,".tbz")) $zipfile = new bzip_file($cmd_arg);
                        elseif (strstr($cmd_arg,".gzip")||strstr($cmd_arg,".gz")||strstr($cmd_arg,".tgz")) $zipfile = new gzip_file($cmd_arg);
                        elseif (strstr($cmd_arg,".tar")) $zipfile = new tar_file($cmd_arg);
                        if ($zipfile){
                            $zipfile->set_options(array('basedir'=>$current_dir,'overwrite'=>1));
                            $zipfile->extract_files();
                        }
                        unset($zipfile);
                        reloadframe("parent",2);
                    }
                }
                break;
            case 8: // delete arq/dir
                if (strlen($cmd_arg)){
                    if (file_exists($current_dir.$cmd_arg)) total_delete($current_dir.$cmd_arg);
                    if (is_dir($current_dir.$cmd_arg)) reloadframe("parent",2);
                }
                break;
            case 9: // CHMOD
                if((strlen($chmod_arg) == 4)&&(strlen($current_dir))){
                    if ($chmod_arg[0]=="1") $chmod_arg = "0".$chmod_arg;
                    else $chmod_arg = "0".substr($chmod_arg,strlen($chmod_arg)-3);
                    $new_mod = octdec($chmod_arg);
                    if (strlen($selected_file_list)){
                        $selected_file_list = explode("<|*|>",$selected_file_list);
                        if (count($selected_file_list)) {
                            for($x=0;$x<count($selected_file_list);$x++) {
                                $selected_file_list[$x] = trim($selected_file_list[$x]);
                                if (strlen($selected_file_list[$x])) @chmod($current_dir.$selected_file_list[$x],$new_mod);
                            }
                        }
                    }
                    if (strlen($selected_dir_list)){
                        $selected_dir_list = explode("<|*|>",$selected_dir_list);
                        if (count($selected_dir_list)) {
                            for($x=0;$x<count($selected_dir_list);$x++) {
                                $selected_dir_list[$x] = trim($selected_dir_list[$x]);
                                if (strlen($selected_dir_list[$x])) @chmod($current_dir.$selected_dir_list[$x],$new_mod);
                            }
                        }
                    }
                }
                break;
        }
        if ($action != 10) dir_list_form();
    } else dir_list_form();
    echo "</body>\n</html>";
    */
}

require("phpfmtpl.php");

?>